#include "RR.h"
#include <stdio.h>
#include <stdlib.h>

/**
 * 函数: calculateTimes
 * 功能: 根据时间片轮转调度算法计算每个进程的完成时间、周转时间和等待时间
 *
 * 参数:
 *  - processes: 指向包含多个进程信息的数组
 *  - n: 进程数量
 *  - time_slice: 时间片长度
 *
 * 描述:
 *  计算进程的完成时间，周转时间和等待时间并存在结构体中即可
 */
void
calculateTimes (Process *processes, int n, int time_slice)
{
  // TODO
  int process_num = n;
  int time = 0;
  int time_all = 0;
  int pid = 0;
  int complete_process = 0;
  while (process_num != 0) {
    if (time == time_slice) {
      time = 0;
      do {
        pid++;
        if (pid == n) pid = 0;
      } while (processes[pid].remaining_time == 0);
    }
    time++;
    processes[pid].remaining_time--;
    time_all++;
    if (processes[pid].remaining_time == 0) {
      processes[pid].completion_time = time_all;
      processes[pid].turnaround_time= time_all - processes[pid].arrival_time;
      processes[pid].waiting_time = processes[pid].turnaround_time - processes[pid].burst_time;
      process_num--;
      do {
        pid++;
        complete_process++;
        if (pid == n) pid = 0;
        if (complete_process == 4) break;
      } while (processes[pid].remaining_time == 0);
      complete_process = 0;
      time = 0;
    }
  }
}